import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:renqing/common/sqlite/util.dart';

class MainManager with ChangeNotifier {
  static MainManager instance = MainManager();
  final sqliteUtil = SqliteUtil();
  List allLocalCache = [];

  void init() async {
    sqliteUtil.createDb(1);
    // clearDb();
    refresh();
  }

  Future queryAllLocalCache() async {
    return query("select * from RenqingList");
  }

  void clearDb() {
    sqliteUtil.delete("DELETE FROM RenqingList");
  }

  Future query(sql) async {
    return sqliteUtil.query(sql);
  }

  void insert(String id, String data) async {
    await sqliteUtil
        .insert("INSERT INTO RenqingList(id,data) VALUES($id,'$data')");
    refresh();
  }

  void update(String id, String data) async {
    await sqliteUtil
        .update('UPDATE RenqingList SET data = ? WHERE id = $id', [data]);
    refresh();
  }

  void delete(String id) async {
    await sqliteUtil.delete('DELETE FROM RenqingList WHERE id = "$id"');
    refresh();
  }

  refresh() {
    queryAllLocalCache().then((res) {
      allLocalCache = res;
      // print(allLocalCache);
      notifyListeners();
    });
  }
}
